package com.tlkg.qa.redistest;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import lombok.extern.log4j.Log4j;

@Log4j
public class StartTest2 {

	public static void main(String[] args) {

		int client = 10; 
		int num = 100000;
		int test = 1;

		for (String string : args) {
			if (string.startsWith("-J")) {
				String[] arg = string.substring(2).split("=");
				switch (arg[0]) {
				case "c":
					client = Integer.parseInt(arg[1]);
					break;
				case "n":
					num = Integer.parseInt(arg[1]);
					break;
				case "t":
					if (arg[1].equals("set")) {
						test = 1;
					} else if (arg[1].equals("get")) {
						test = 2;
					} else {
						System.err.println("参数有误！");
						System.exit(0);
					}
					break;
				default:
					break;
				}
			}
		}

		int subNum = num / client;

		log.info("test start! " + new Date());
		log.info("summary: " + num + " request ," + client + " client");

		long start = System.nanoTime();

		Vector<Thread> threads = new Vector<Thread>();

		for (int i = 1; i <= client; i++) {
			TestThead2 testThead = new TestThead2("testThread-" + i);
			testThead.setTest(test);
			testThead.setNum(subNum);
			threads.add(testThead);
			testThead.start();
		}

		for (Thread iThread : threads) {
			try {
				// 等待所有线程执行完毕
				iThread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		long end = System.nanoTime();

		float duration = (end - start) / 1000000;
		log.info("测试执行完毕！ " + new Date());
		log.info("持续时间：" + duration + " ms");
		log.info("QPS：" + (num / duration) * 1000);
		if (test == 1) {
			log.info("Set未命中次数" + Count.setmisscount.get());
		} else if (test == 2) {
			log.info("Get未命中次数" + Count.getmisscount.get());
		}

//		List<Long> list = new ArrayList<Long>(Count.sortset);
//
//		Long p50 = list.get((int) (list.size() * 0.5));
//		Long p90 = list.get((int) (list.size() * 0.9));
//		Long p95 = list.get((int) (list.size() * 0.95));
//		Long p99 = list.get((int) (list.size() * 0.99));
//
//		log.info("\n50%:" + p50 + "ns\n 90%:" + p90 + "ns\n 95%:" + p95 + "ns\n 99%:" + p99);
	}
}